
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    M: "",
    border: "4rpx solid #ff3f80",
    background: "#11b7f3",
    state: "申请",
    // 
    payFlag: true,
    orderNumber: "",
    payType: "",
    // 
    circleArr: [{
      value: "",
      active: false
    },
    {
      value: "",
      active: false
    },
    {
      value: "",
      active: false
    },
    {
      value: "",
      active: false
    }
    ],
    borderArr: [{
      value: "",
      active: false
    },
    {
      value: "",
      active: false

    },
    {
      value: "",
      active: false

    },
    ],
    // 
    uid: "",
    userType: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",
    //不定额预支去支付钱数
    payFee: "",
  },

  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.BigCustomerApplyMaList();

  },

  //显示支付密码输入层
  showInputLayer: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function () {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function (e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function () {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=BigCustomerApplyMaFeePay&OrderNumber=" + orderNumber + "&PayType=1"),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultData == "success") {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // inputM
  inputM: function (e) {
    this.setData({
      M: e.detail.value,
    })
  },
  BigCustomerApplyMaList: function () {
    var that = this;
    var uid = this.data.uid;
    var state = this.data.state;
    var circleArr = this.data.circleArr;
    var borderArr = this.data.borderArr;
    var background = this.data.background;
    var orderNumber = this.data.orderNumber;
    var payFee = this.data.payFee;
    var M = this.data.M;
    var border = this.data.border;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BigCustomerApplyMaList&UID=" + uid),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultData.length == 0) {
          that.setData({
            state: "申请",
          })
        } else {
          if (json.ResultData[json.ResultData.length - 1].Status == 0) {
            for (var i = 0; i < 1; i++) {
              borderArr[i].active = true;
            }
            for (var i = 0; i < 2; i++) {
              circleArr[i].active = true;
            }
            M = json.ResultData[json.ResultData.length - 1].BorrowMa;
            border = "4rpx solid #f6f6f6";
            background = "#c6c6c6";
            that.setData({
              state: "待审核",
              borderArr: borderArr,
              circleArr: circleArr,
              M: M,
              border: border,
              background: background,
            })
          } else if (json.ResultData[json.ResultData.length - 1].Status == 1) {
            for (var i = 0; i < 2; i++) {
              borderArr[i].active = true;
            }
            for (var i = 0; i < 3; i++) {
              circleArr[i].active = true;
            }
            M = json.ResultData[json.ResultData.length - 1].BorrowMa;
            border = "4rpx solid #f6f6f6";
            orderNumber = json.ResultData[json.ResultData.length - 1].OrderNumber;
            payFee = parseFloat(json.ResultData[json.ResultData.length - 1].PayFee).toFixed(2);
            that.setData({
              borderArr: borderArr,
              circleArr: circleArr,
              state: "去支付",
              M: M,
              border: border,
              orderNumber: orderNumber,
              payFee: payFee,
            })
          } else if (json.ResultData[json.ResultData.length - 1].Status == 3) {
            that.setData({
              state: "再次申请",
            })
          }
        }
      }
    })
  },
  // stateTap
  stateTap: function () {
    var that = this;
    var state = this.data.state;
    if (state == "申请") {
      // 第一次申请
      this.BigCustomerApplyMa();
    } else if (state == "待审核") {
    } else if (state == "去支付") {
      this.setData({
        payFlag: false,
      })
    } else if (state == "再次申请") {
      this.BigCustomerApplyMa();
    } else {
    }
  },
  // BigCustomerApplyMa
  BigCustomerApplyMa: function () {
    var that = this;
    var uid = this.data.uid;
    var M = this.data.M;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BigCustomerApplyMa&UID=" + uid + "&BorrowMa=" + M),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: "申请成功！",
            icon: 'none',
            duration: 2000
          });
          that.BigCustomerApplyMaList();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // cancelPay
  cancelPay: function () {
    this.setData({
      payFlag: true,
    })
  },
  //余额或微信 
  radioChange: function (e) {
    var payType = this.data.payType;
    payType = e.detail.value
    this.setData({
      payType: payType,
    });
  },
  // goPay
  goPay: function () {
    var that = this;
    var orderNumber = this.data.orderNumber;
    var payType = this.data.payType;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (payType == 1) {
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true,
          payFlag: true,
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=BigCustomerApplyMaFeePay&OrderNumber=" + orderNumber + "&PayType=3"),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
          console.log(json);
          if (json.ResultFlag == 1) {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     console.log(res);
            //     wx.navigateBack({
            //       delta: 1
            //     })
            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    }
  },
})